/**
 *  二分查找
 */
function binarySearch(arr, target) {
    const length = arr.length;
    
    let left = 0,
        right = length - 1;

    let res = -1;

    while (left <= right) {
        let mid = (left + right) >> 1;
        if (arr[mid] == target) {
            res = mid;
            break;
        }
        else if (arr[mid] > target) {
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }
    return res;
}

module.exports.binarySearch = binarySearch;
